import { isString, isUndefined } from 'lodash-es'

import { viteEnv } from '@/config'
/** 页面 title hooks */
export function useHtmlTitle() {
  const { VITE_APP_TITLE } = viteEnv
  const setHtmlTitle = (title?: MetaTitle) => {
    if (!title) {
      document.title = VITE_APP_TITLE
      return
    }
    if (isString(title) && title) {
      document.title = `${VITE_APP_TITLE} - ${title}`
      return
    }
    const { text, append, separator } = title as MetaTitleObj
    const isAppend = isUndefined(append) ? true : append
    let titleText = VITE_APP_TITLE
    if (isString(text) && text) {
      titleText = isAppend
        ? `${VITE_APP_TITLE} ${separator ?? '-'} ${text}`
        : text
    }
    document.title = titleText
  }
  return { setHtmlTitle }
}
